#============================================================
#    Base Role
#============================================================
# - datetime: 2023-01-10 23:27:05
#============================================================
## 基础的角色
class_name BaseRole
extends CharacterBody2D


## 受到伤害
signal got_hurt(data: Dictionary)
## 死亡
signal died(data: Dictionary)


@onready
var property_management : PropertyManagement = %property as PropertyManagement
@onready 
var health_bar: ProgressBar = %health_bar
@onready 
var move_controller := %move_controller as MoveController


#============================================================
#  内置
#============================================================
func _ready() -> void:
	# 新增这个属性时
	Aspect.around(property_management.newly_added_property, func(property, value):
		match property:
			Const.Property.MOVE_SPEED:
				move_controller.move_speed = value
		
	)
	
	# 属性发生改变之前
	Aspect.brefore(property_management.property_changed, func(property, previous, current):
		match property:
			Const.Property.HEALTH:
				# 生命值不能小于 0
				if current < 0:
					# 打断信号，不继续执行连接的方法，并修改 health 为 0
					Aspect.prevent_signal(property_management.property_changed)
					property_management.set_property(Const.Property.HEALTH, 0)
			
	, -1000)
	
	# 连接属性改变信号，监听属性，来处理一些内容
	Aspect.around(property_management.property_changed, func(property, previous, current):
		match property:
			
			Const.Property.HEALTH:
				# 生命条修改
				if health_bar.max_value < current:
					health_bar.max_value = current
				health_bar.value = current
			
			Const.Property.MOVE_SPEED:
				move_controller.move_speed = current
		
	)
	
	Aspect.after(property_management.property_changed, func(property, previous, current):
		# 如果生命值小于等于0，则发出 died 信号
		if property == Const.Property.HEALTH:
			if current <= 0:
				self.died.emit({})
	)
	
	# 移动
	Aspect.around(move_controller.moved, func(vector):
		self.velocity = vector
		self.move_and_slide()
	)
